#include "Sound.h"
Sound::Sound()
{
	_sizeFile = NULL;
	_device = -1;
	_flag = 0;
	_freg = 44100;
	_streams = NULL;
	_streamCount = 0;
}
Sound::~Sound()
{
	RemoveAll();
}
int Sound::Init(HWND hwnd)
{
	this->_hwnd = hwnd;
	_device = -1;
	_freg = 44100;
	_flag =0;
	if (HIWORD(BASS_GetVersion()) != BASSVERSION) {
		MessageBox(hwnd, "An incorrect version of BASS.DLL was loaded", "Error", MB_OK | MB_ICONERROR);
		return false;
	}

	if (!BASS_Init(_device, _freg, _flag, hwnd, NULL)) {
		MessageBox(hwnd, "Can't initialize device", "Error", MB_OK | MB_ICONERROR);
		return false;
	}

	return true;
}
int Sound::AddFile(STREAM_TYPE type, const char* sID, const char* szFile, DWORD flags)
{
	for (int i=0; i<_streamCount ; i++)
	{
		if(strcmp(sID,_streams[i].sID) == 0)
		{
			MessageBox(_hwnd, "File da them vao roi","Error",MB_OK | MB_ICONERROR);
			return false;
		}
	}
	DWORD hSound;
	if(type == OTHERFILES)
	{
		if(hSound = BASS_StreamCreateFile(FALSE, szFile, 0, 0, flags))
		{
			_streamCount ++;
			_streams[_streamCount -1 ].hStream = hSound;
			_streams[_streamCount -1 ].idType = type;
			_streams[_streamCount - 1].sID = (char*)malloc(sizeof(sID));
			strcpy(_streams[_streamCount - 1 ].sID,sID);
		}
		else
		{
			MessageBox(_hwnd, "khong the load file !", "Error", MB_OK | MB_ICONERROR);
			return false;
		}

	}
	else if(type == MOD)
	{

		if (hSound = BASS_MusicLoad(FALSE, szFile, 0, 0, flags, 0)) 
		{
			_streamCount++;
			_streams = (PSTREAM_NODE)realloc((void*)_streams, _streamCount*sizeof(STREAM_NODE));

			_streams[_streamCount - 1].hStream	= hSound;
			_streams[_streamCount - 1].idType	= type;
			_streams[_streamCount - 1].sID = (char*)malloc(sizeof(char) * strlen(sID));
			strcpy(_streams[_streamCount - 1].sID, sID);
		} else 
		{
			MessageBox(_hwnd, "khong the load file!", "Error", MB_OK | MB_ICONASTERISK);
			return false;
		}
	}
	return true;
}
void Sound::ReamoveFile(char *id)
{
	int pos;
	int found = 0;
	for(pos = 0; pos < _streamCount ; pos ++)
	{
		if(strcmp(_streams[pos].sID , id) == 0)
		{
			found = 1;
			break;
		}
	}
	if(found == 0)
		return;

	if(_streams[pos].idType == OTHERFILES)
	{
		BASS_StreamFree(_streams[pos].hStream);
		_streamCount --;
		memcpy(_streams + pos, _streams + pos + 1, (_streamCount - pos)*sizeof(STREAM_NODE));
	}else if(_streams[pos].idType == MOD)
	{
		BASS_MusicFree(_streams[pos].hStream);
		_streamCount--;
		memcpy(_streams + pos, _streams + pos + 1, (_streamCount - pos)*sizeof(STREAM_NODE));
	}
}
void Sound::RemoveAll()
{
	int pos;
	for (pos = 0; pos<_streamCount ; pos++)
	{
		if (_streams[pos].idType == OTHERFILES)
		{
			BASS_StreamFree(_streams[pos].hStream);
			_streamCount--;
			memcpy(_streams + pos, _streams + pos + 1, (_streamCount - pos)*sizeof(STREAM_NODE));
		} else if (_streams[pos].idType == MOD)
		{
			BASS_MusicFree(_streams[pos].hStream);
			_streamCount--;
			memcpy(_streams + pos, _streams + pos + 1, (_streamCount - pos)*sizeof(STREAM_NODE));
		}
	}
}

int Sound::Play(char *id, bool reStart)
{
	int pos;
	int found = 0;
	for (pos = 0; pos < _streamCount ; pos++)
	{
		if(strcmp(_streams[pos].sID,id) == 0)
		{
			found = 1;
			break;
		}
	}
	if(found == 0)
		return false;
	if(!BASS_ChannelPlay(_streams[pos].hStream,reStart))
	{
		MessageBox(_hwnd, "khong the play !", 
			"Error", MB_OK | MB_ICONERROR);
		return false;
	}

	return true;
}
void Sound::Pause(char *id)
{
	int pos;
	int found = 0;
	for (pos = 0; pos <_streamCount ; pos++)
	{
		if(strcmp(_streams[pos].sID ,id) == 0)
		{
			found = 1;
			break;
		}
	}
	if(found == 0)
		return ;
	BASS_ChannelPause((_streams[pos].hStream));
}

void Sound::Stop(char *id)
{
	int pos, found = 0;
	for (pos = 0; pos <_streamCount ; pos++)
	{
		if(strcmp(_streams[pos].sID ,id) == 0)
		{
			found = 1;
			break;
		}
	}
	if(found == 0)
		return ;
	BASS_ChannelStop(_streams[pos].hStream);
}

void Sound::PauseAll()
{
	BASS_Pause();
}

void Sound::StopAll()
{
	BASS_Stop();
}
void Sound::ResumeAll()
{
	BASS_Start();
}
void Sound::SetGlobalVolume (int nVol)
{
	BASS_SetVolume((float)nVol/100.0f);
}
void Sound::SetItemVolume (char* id, int vol)
{
	int nPos, nFound = 0;
	for (nPos = 0; nPos < _streamCount; nPos++)
	{
		if (0 == strcmp(_streams[nPos].sID, id))
		{
			nFound = 1;
			break;
		}
	}
	if (nFound == 0){
		return;
	}
	BASS_ChannelSetAttribute(_streams[nPos].hStream, BASS_ATTRIB_VOL,  (float)vol/100);
}